package jdk7;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryWithResources {
	/**
	 * try-with-resource 不需要使用finally来保证打开的流被正确关闭 这是自动完成的。
	 */
	public class ResourceBasicUsage {

		public String readFile(String path) throws IOException {
			// try-with-resource
			try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
				StringBuilder builder = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
					builder.append(String.format("%n"));
				}
				return builder.toString();
			}
		}
	}
}
